<?php

$NRIC = $_REQUEST['barcode'];
$date = $_SESSION['selectedBackDate'];
$Username = $_SESSION['loginusername'];
$userCenterName = $_SESSION["centreName"];
$centreName = "";


include 'conn.php';
$conn = mysql_connect("127.0.0.1", "root", "") or die("Can't connect to database");
$db = mysql_select_db("eldercaredb", $conn);
$qry = "Select CentreName from centre where CentreId = (SELECT client.CentreId from centre, client WHERE client.NRIC = '$NRIC' AND centre.CentreId = client.CentreId)";
$result1 = mysql_query($qry);
while ($row = mysql_fetch_array($result1)) {
    $centreName = $row['CentreName'];
}


$result = mysql_query("Select * from attendancerecord where NRIC='$NRIC' AND DATE(SignInTimeStamp) like '$date%'");
if ($centreName == $userCenterName) {
    if (mysql_num_rows($result) == 0) {
        $signInTimeStamp = $date . ' 00:00:00';
        $signOutTimeStamp = $date . ' 00:00:00';
        $sql = "INSERT INTO attendancerecord (NRIC, CentreId, SignInTimeStamp, SignOutTimeStamp, Status, AuditSignIn) 
              VALUES ( '$NRIC',
              (
              SELECT client.CentreId 
              FROM  client
              WHERE NRIC = '$NRIC'), '$signInTimeStamp','$signOutTimeStamp','BackDated', '$Username')";
        $result = @mysql_query($sql);

        if ($result) {
            echo "<font color='red'>Back Date Success!</font>";
        } else {
            echo "<font color='red'>Back Date NOT Successful!</font>";
        }
    } else {
        echo "<font color='red'>Attendance for that elderly has been taken already!</font>";
    }
} else {
    echo "<font color='red'>The elderly does not belong to this centre!</font>";
}
?>	